import { toEqual } from 'tests/utils';

import loess from './loess';

const data: [number, number][] = [
  [1900, 57.5605],
  [1901, 57.3214],
  [1902, 56.2134],
  [1903, 56.317],
  [1904, 57.4467],
  [1905, 57.1332],
  [1906, 57.2392],
  [1907, 56.5545],
  [1908, 56.2721],
  [1909, 56.2912],
  [1910, 57.7532],
  [1911, 55.474],
  [1912, 55.5628],
  [1913, 56.5263],
  [1914, 57.4455],
  [1915, 56.8189],
  [1916, 55.5932],
  [1917, 56.6997],
  [1918, 56.9079],
  [1919, 56.3611],
  [1920, 56.1257],
  [1921, 57.3778],
  [1922, 56.1452],
  [1923, 56.4466],
  [1924, 57.1546],
  [1925, 57.206],
  [1926, 58.8471],
  [1927, 56.94],
  [1928, 57.7148],
  [1929, 57.3422],
  [1930, 56.9745],
  [1931, 58.3638],
  [1932, 56.9607],
  [1933, 56.8247],
  [1934, 59.7805],
  [1935, 56.8945],
  [1936, 58.6637],
  [1937, 57.2449],
  [1938, 57.3329],
  [1939, 58.4247],
  [1940, 58.8798],
  [1941, 57.1222],
  [1942, 57.1411],
  [1943, 57.894],
  [1944, 56.2842],
  [1945, 57.1573],
  [1946, 56.8022],
  [1947, 57.6003],
  [1948, 55.6891],
  [1949, 56.2792],
  [1950, 58.1899],
  [1951, 57.2899],
  [1952, 56.8036],
  [1953, 57.2433],
  [1954, 57.6142],
  [1955, 56.3011],
  [1956, 57.0172],
  [1957, 57.1288],
  [1958, 58.9603],
  [1959, 59.0416],
  [1960, 58.0866],
  [1961, 57.8652],
  [1962, 57.4384],
  [1963, 57.1077],
  [1964, 56.6202],
  [1965, 56.774],
  [1966, 58.0992],
  [1967, 57.5668],
  [1968, 57.5989],
  [1969, 57.5899],
  [1970, 57.8767],
  [1971, 56.2718],
  [1972, 57.2934],
  [1973, 57.3641],
  [1974, 57.7036],
  [1975, 56.3447],
  [1976, 57.5107],
  [1977, 58.0088],
  [1978, 57.6893],
  [1979, 57.7485],
  [1980, 58.1052],
  [1981, 59.3551],
  [1982, 56.4003],
  [1983, 57.6184],
  [1984, 58.2609],
  [1985, 57.3929],
  [1986, 58.9427],
  [1987, 58.3293],
  [1988, 58.7852],
  [1989, 58.1885],
  [1990, 58.0488],
  [1991, 58.16],
  [1992, 59.2918],
  [1993, 57.7408],
  [1994, 58.1055],
  [1995, 58.9677],
  [1996, 59.594],
  [1997, 59.1923],
  [1998, 56.7942],
  [1999, 58.0356],
  [2000, 58.8363],
  [2001, 59.2216],
  [2002, 58.8964],
  [2003, 59.529],
  [2004, 58.9128],
  [2005, 58.6833],
  [2006, 58.6975],
  [2007, 58.9929],
  [2008, 58.9459],
  [2009, 58.917],
  [2010, 57.7926],
  [2011, 57.5186],
  [2012, 59.5448],
  [2013, 59.3921],
  [2014, 61.5208],
  [2015, 60.7992],
  [2016, 60.1429],
  [2017, 60.4208],
];

describe('[loess calculation]', () => {
  it('loess(data, bandwidth) calculates the LOESS regression', () => {
    function shuffle(arr: [number, number][]) {
      let m = arr.length,
        t,
        i;
      while (m) {
        i = Math.floor(Math.random() * m--);
        t = arr[m];
        arr[m] = arr[i];
        arr[i] = t;
      }
      return arr;
    }

    const r = loess(shuffle(data), 0.3);

    const output = [
      [1900, 56.842036038120746],
      [1901, 56.82388042072633],
      [1902, 56.8070189828568],
      [1903, 56.791439968282916],
      [1904, 56.777183782649004],
      [1905, 56.76433479172034],
      [1906, 56.75292962790576],
      [1907, 56.74294889153739],
      [1908, 56.73431787165204],
      [1909, 56.72695170364497],
      [1910, 56.72078756696423],
      [1911, 56.71576021371101],
      [1912, 56.711699920350995],
      [1913, 56.708473636333565],
      [1914, 56.7060880931565],
      [1915, 56.70480534893961],
      [1916, 56.70576431788093],
      [1917, 56.71232026149931],
      [1918, 56.747900012306594],
      [1919, 56.789420693808246],
      [1920, 56.83748213534298],
      [1921, 56.89225052264274],
      [1922, 56.95199450220717],
      [1923, 57.014438615650704],
      [1924, 57.07859385148974],
      [1925, 57.14473392742243],
      [1926, 57.21214134229593],
      [1927, 57.27901032976046],
      [1928, 57.343086401323646],
      [1929, 57.40283047680427],
      [1930, 57.45672812128807],
      [1931, 57.50254743337572],
      [1932, 57.5382672855882],
      [1933, 57.561956392439235],
      [1934, 57.57327443318934],
      [1935, 57.57348137525841],
      [1936, 57.56400274816263],
      [1937, 57.54670946886326],
      [1938, 57.52367643827728],
      [1939, 57.49687396721632],
      [1940, 57.46813369606984],
      [1941, 57.43860331605789],
      [1942, 57.40872244047538],
      [1943, 57.37987901554629],
      [1944, 57.35366139781617],
      [1945, 57.331848081812225],
      [1946, 57.315396344090935],
      [1947, 57.30449888731605],
      [1948, 57.29847592437323],
      [1949, 57.2955374044323],
      [1950, 57.29521879323507],
      [1951, 57.298018542818255],
      [1952, 57.303440826136594],
      [1953, 57.31226477763196],
      [1954, 57.32510063614931],
      [1955, 57.34264762617605],
      [1956, 57.363678118397104],
      [1957, 57.38591358081206],
      [1958, 57.40755388563386],
      [1959, 57.42815511755062],
      [1960, 57.44632106674749],
      [1961, 57.46071240516167],
      [1962, 57.471416346181],
      [1963, 57.478388358377934],
      [1964, 57.48179755794645],
      [1965, 57.48281654883124],
      [1966, 57.4843219223156],
      [1967, 57.48668592399354],
      [1968, 57.488693587174154],
      [1969, 57.490265177390114],
      [1970, 57.49205947840186],
      [1971, 57.49573834430299],
      [1972, 57.503546856084725],
      [1973, 57.518706091853566],
      [1974, 57.54298958003794],
      [1975, 57.575914537813304],
      [1976, 57.61500221122251],
      [1977, 57.65826392696781],
      [1978, 57.703917579810366],
      [1979, 57.7507160415611],
      [1980, 57.79897718774289],
      [1981, 57.85045711635498],
      [1982, 57.90635482156091],
      [1983, 57.96475127434016],
      [1984, 58.022715613240194],
      [1985, 58.07845598392329],
      [1986, 58.1311956998041],
      [1987, 58.18085072813873],
      [1988, 58.22812103454775],
      [1989, 58.27418703914001],
      [1990, 58.31918578044981],
      [1991, 58.36292543303682],
      [1992, 58.40569657511456],
      [1993, 58.44831291989403],
      [1994, 58.490331693451246],
      [1995, 58.53005874798704],
      [1996, 58.565725371856644],
      [1997, 58.59657656710402],
      [1998, 58.62266138294421],
      [1999, 58.64611783088632],
      [2000, 58.67140622932588],
      [2001, 58.71065577244432],
      [2002, 58.762536824661204],
      [2003, 58.820539956595965],
      [2004, 58.88112858108126],
      [2005, 58.94258080709306],
      [2006, 59.00406425615884],
      [2007, 59.0652053660954],
      [2008, 59.125872778795475],
      [2009, 59.18605858797359],
      [2010, 59.24583648641658],
      [2011, 59.30536787973602],
      [2012, 59.36498198843687],
      [2013, 59.425152996051466],
      [2014, 59.4863324246197],
      [2015, 59.54888991301135],
      [2016, 59.61298575584295],
      [2017, 59.67862753979371],
    ];

    toEqual(r, output);
  });
});
